package com.xbongbong.parent.abstracts;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.parent.interfaces.BusinessDetailBaseInfoService;
import com.xbongbong.parent.interfaces.BusinessDetailTopInfoService;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2021/12/30 14:06
 */
@Component
public abstract class AbstractBusinessDetailBaseInfoService<T, D> extends AbstractParentBusinessDetailService<T, D> implements BusinessDetailBaseInfoService<T, D> {

    @Resource
    private Map<Integer, BusinessDetailTopInfoService> businessDetailTopInfoMap;

    @Override
    public FormDataGetVO get(FormDataGetDTO formDataGetDTO) throws XbbException {
        String corpid = formDataGetDTO.getCorpid();
        BusinessDetailTopInfoService businessDetailTopInfoService = businessDetailTopInfoMap.get(formDataGetDTO.getBusinessType());

        // 获取数据
        D formData = getData(formDataGetDTO.getBusinessType(), formDataGetDTO.getSubBusinessType(), formDataGetDTO.getSaasMark(), corpid, formDataGetDTO.getDataId(), formDataGetDTO.getFormId());
        // 获取表单
        T form = getForm(formDataGetDTO.getLoginUser(), formData, corpid);
        // 获取表单解释
        List<FieldAttrEntity> explainList = getExplainList(form, corpid);
        // 解析字段解释
        HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
        BeanUtil.copyProperties(formDataGetDTO, handlerExplainDTO, false);
        HandlerExplainVO handlerExplainVO = handleExplain(explainList, form, formData, handlerExplainDTO);
        // 特殊解释
        handleDetailSpecialExplain( formData, handlerExplainVO.getHeadList());
        // 解析数据
        JSONObject data = analysisData(formData, handlerExplainDTO, handlerExplainVO);
        // 千分位换算
        analysisGetOfThousandBits(data, handlerExplainVO.getHeadList(), false);
        // 返回封装
        FormDataGetVO formDataGetVO =  returnVo(formData, handlerExplainVO, data, form, formDataGetDTO.getUserId(), formDataGetDTO.getCorpid());
        JSONObject returnJson = businessDetailTopInfoService.returnSaasObject(corpid, formDataGetDTO.getBusinessType(), formDataGetDTO.getLoginUser(), formData);
        returnJson = returnJson == null ? new JSONObject() : returnJson;
        formDataGetVO.setSaasObj(returnJson);
        return formDataGetVO;
    }
}
